package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.entity.RequirementEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.PurchaserInfoMapper;
import com.antler.smt.portal.mapper.RequirementEntityMapper;
import com.antler.smt.portal.mapper.RequirementInfoMapper;
import com.antler.smt.request.admin.GetRequirementListReq;
import com.antler.smt.response.admin.GetPurchaserListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.RequirementService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.LogUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 发布需求接口实现
 *
 * @author zhangyi
 * @date 2018/3/23 10:56
 */
@Service
public class RequirementServiceImpl implements RequirementService {

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Autowired
    private RequirementEntityMapper requirementEntityMapper;

    @Autowired
    private RequirementInfoMapper requirementInfoMapper;

    @Autowired
    private PurchaserInfoMapper purchaserInfoMapper;

    /**
     * 写入发布需求
     *
     * @param request 获取用户需求信息实体
     * @param memberId 会员id
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result<Long> insertRequirement(GetRequirementListReq request,long memberId) {

        //发布需求实体
        RequirementEntity requirement = new RequirementEntity();

        //获取采购方实体
        GetPurchaserListResp getPurchaserResps = purchaserInfoMapper.selectByMemberId(memberId);

        //添加采购方ID
        requirement.setPurchaserId(getPurchaserResps.getId());

        //添加用户填充数据
        requirement.setName(request.getName());
        requirement.setDescription(request.getDescription());
        requirement.setType(request.getType());

        //添加单点套数
        requirement.setDdtsHp0402(request.getDdtsHp0402());
        requirement.setDdtsBga(request.getDdtsBga());
        requirement.setDdtsDipDls(request.getDdtsDipDls());
        requirement.setDdtsDipHp(request.getDdtsDipHp());
        requirement.setDdtsDipHx(request.getDdtsDipHx());
        requirement.setDdtsOther(request.getDdtsOther());

        //添加信息物料
        requirement.setWlCount(request.getWlCount());
        requirement.setWlBz(request.getWlBz());
        requirement.setWlCl(request.getWlCl());
        requirement.setWlJj(request.getWlJj());
        requirement.setWlQw(request.getWlQw());
        requirement.setWlTz(request.getWlTz());
        requirement.setWlGwAttachmentId(request.getWlGwAttachmentId());

        requirement.setWlJjAttachmentId(request.getWlJjAttachmentId());

        //添加交期类型
        requirement.setEndlineType(request.getEndlineType());
        //时间转换
        requirement.setEndline(request.getEndline());

        //添加标准文件 返回文件id中获取信息
        requirement.setBzwjClqdAttachmentId(request.getBzwjClqdAttachmentId());
        requirement.setBzwjTzAttachmentId(request.getBzwjTzAttachmentId());
        requirement.setBzwjZbwjAttachmentId(request.getBzwjZbwjAttachmentId());

        //添加测量类型
        requirement.setAoi(request.getAoi());

        //添加报价
        requirement.setOffer(request.getOffer());

        //添加发货时间
        requirement.setSendTime(request.getSendTime());

        //添加收货地址id
        requirement.setAddressId(request.getAddressId());

        //添加收获人信息
        requirement.setContactTel(request.getContactTel());

        //添加联系人
        requirement.setContactMan(request.getContactMan());

        //添加备注
        requirement.setRemarks(request.getRemarks());

        //添加创建时间时间
        requirement.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //添加更新时间
        requirement.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //防止多次提交发布需求:根据时间戳
        Date createTime = requirement.getCreateTime();
        //此时发布的时间戳
        long time = createTime.getTime();
        //获取最后一次次采购商插入的时间
        Date lastTimeDate = requirementInfoMapper.getLastTimeDate(getPurchaserResps.getId());
        if(lastTimeDate != null) {
            long lastTime = lastTimeDate.getTime();
            //一分钟定时
            long oneMin = 1000*60;
            //在插入后的1分钟内不允许插入
            if(time-lastTime > oneMin){
                int i = requirementEntityMapper.insertSelective(requirement);
                //获取最后一次插入的id
                Long aLong = requirementInfoMapper.selectLastId();
                //检查插入状态
                boolean flag = (0 == i);

                if(!flag) {

                    return new Result(aLong);
                }else {
                    //失败返回id 为 0
                    return new Result(0);
                }
            }else{
                return new Result(ResultCodeEnum.MODIFY_DATA_ERROR,"不允许重复插入，1分钟后再试！");
            }
        }else{
            int i = requirementEntityMapper.insertSelective(requirement);
            //获取最后一次插入的id
            Long aLong = requirementInfoMapper.selectLastId();
            //检查插入状态
            boolean flag = (0 == i);

            if(!flag) {

                return new Result(aLong);
            }else {
                //失败返回id 为 0
                return new Result(0);
            }
        }

    }

    /**
     * 查看需求详情
     *
     * @param id
     * @return
     */
    @Override
    public Result<RequirementEntity> getRequirementInfo(Long id) {
        Result result = null;

        result = new Result(requirementEntityMapper.selectByPrimaryKey(id));

        return result;
    }
}
